package top.hepingan.java.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    // 排除无需登录的路径
    String[] writeUrl = new String[]{
            "/system/wx/revice",
            "/user/login",
            "/user/register",
            "/user/wx-login",
            "/forum/list",
            "/activity/list",
            "/shop/list",
            "/system/carousel/list",
            "/forum/getById",
            "/forum/commentById"
    };

    // 管理后台需要 admin 角色的路径
    String[] adminUrl = new String[]{
            "/admin/**"
    };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> StpUtil.checkLogin()))
                .excludePathPatterns(writeUrl);

        registry.addInterceptor(new SaInterceptor(handler -> StpUtil.checkRole("admin")))
                .addPathPatterns(adminUrl);
    }


}
